package tk.mybatis.springboot.api.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import tk.mybatis.springboot.model.Message;
import tk.mybatis.springboot.service.PayService;
import tk.mybatis.springboot.wexin.util.XmlUtil;

@Controller
@RequestMapping("/pay")
public class PayController extends BaseController{
	@Autowired
	private  PayService payService;
    @RequestMapping(method = RequestMethod.POST ,value = "gateway")
    @ResponseBody
	public Message gateway(HttpServletRequest request, HttpServletResponse response, String service, String orderId, String callback_url) throws Exception{
    	return payService.gateway(service, orderId, callback_url);
    }
    
    @RequestMapping(method = RequestMethod.GET, value = "gateway/result")
    @ResponseBody
	public String successSend(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	String respXml = null;
		// 调用parseXml方法解析请求消息
		Map<String, String> requestMap = XmlUtil.parseXml(request);
		System.out.println("微信发送的xml为：");
		for (String key : requestMap.keySet()) {
			System.out.println(key + "：" + requestMap.get(key));
		}
    	return "success";
    }
    
    @RequestMapping(method = RequestMethod.GET, value = "refund")
    @ResponseBody
	public Message refund(HttpServletRequest request, HttpServletResponse response, String orderId) throws Exception{
    	return payService.refund(orderId);
    }
}
